<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="emp.name">
  <input type="text" placeholder="请输入员工工资" v-model="emp.salary">
  <input type="text" placeholder="请输入员工职位" v-model="emp.job">
  <button @click="add">添加员工</button>
  <hr>
  <table border="1">
    <tr>
      <th>编号</th>
      <th>姓名</th>
      <th>工资</th>
      <th>职位</th>
      <th>操作</th>
    </tr>
    <tr v-for="(item,index) in arr">
      <td>{{index+1}}</td>
      <td>{{item.name}}</td>
      <td>{{item.salary}}</td>
      <td>{{item.job}}</td>
      <td><button @click="arr.splice(index,1)">删除</button></td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";
const uname = ref("");
const salary = ref("");
const job = ref("");
//1.定义对象用来保存员工数据
const emp = ref({name:'', salary:'', job:''});
const arr = ref([]);
const add = ()=>{
  //优化:输入的内容不能为空
  if(emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    alert('请输入完整数据!');
    return;
  }
  arr.value.push(emp.value)
  emp.value = {};
  console.log(arr.value);
}

</script>

<style scoped>

</style>